import { getOrders } from '../../utils/order';
import { formatTime } from "../../utils/util"
import createRecycleContext from "../../compoment/virtualList/index"
Page({
  data: {
    ways: ['全部', '我收藏的', '店家发布', '个人发布'],
    wayIndex: 0,
    time: new Date(),
    initList: [],
    name: ""

  },
  selectCondition(e) {
    wx.showLoading({
      title: 'loading',
      mask: true
    })
    const { id } = e.target;
    const { value } = e.detail;
    switch (id) {
      case "way":
        this.setData({ wayIndex: value }); break;
      case "name":
        this.setData({ name: value }); break;
      case "date":
        this.setData({ timme: value }); break;
      // case "load":
      // default:
      //    ;
    }
    setTimeout(() => {
      wx.hideLoading()
    }, 1000);
  },
  goTask(e) {
    console.log(wx.navigateTo({
      url: '/pages/create/create',
    }))
  },
  reload() {
    wx.showLoading({
      title: 'loading',
    })
    getOrders(({ data }) => {
      this.setData({ products: data.splice(1, 20) })
      wx.hideLoading()
    })
  },
  scroll(e) {
    this.updateList(e.detail.scrollTop, this.data.initList)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  updateList(scrollTop = 0, data) {
    var ctx = createRecycleContext({
      id: 'recycleId',
      dataKey: 'recycleList',
      page: this,
      itemSize: {
        width: 365,
        height: 153
      }
    })
    const query = wx.createSelectorQuery();
    query.select("#recycleId").boundingClientRect(rect => {
      const { height } = rect;
      const count = Math.ceil(height * 2 / 365)
      const start = Math.floor(scrollTop * 2 / 365);
      const end = start + count + 1;
      ctx.append(data.splice(start, end))
    }).exec()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    getOrders(({ data }) => {
      this.updateList(0, data.map(item => ({ ...item, startTime: formatTime("YYYY-mm-dd HH:MM", new Date(item.create_time)) })))
      this.setData({ initList: data.map(item => ({ ...item, startTime: formatTime("YYYY-mm-dd HH:MM", new Date(item.create_time)) })) })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({ selected: 1 })
    }
    wx.showLoading({
      title: 'loading',
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 500);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('88888')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})